package com.xiyuan.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;


import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class ResourceUtil {
    public static Map<String, Object> getYml(Class<?> clazz) {
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        try {
            // 读取YAML文件
            InputStream resourceAsStream = clazz.getClassLoader().getResourceAsStream("interface.yml");
            //不为空就进行下面的操作
            if (resourceAsStream != null) {
                return objectMapper.readValue(resourceAsStream, Map.class);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断路径是否包含这个值
     * & 表示 & 符号前后的字符都要包含
     * | 表示 | 符号前后只要包含一个就可以
     *
     * @param path
     * @return
     */
    public static boolean checkPathContain(String path, String rule) {
        if (rule.contains("&")) {
            String[] split = rule.split("&");
            for (String s : split) {
                if (!path.contains(s))
                    return false;
            }
            return true;
        } else if (rule.contains("|")) {
            String[] split = rule.split("|");
            for (String s : split) {
                if (path.contains(s))
                    return true;
            }
            return false;
        } else {
            return path.contains(rule);
        }

    }
}
